<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="策略名称" prop="strategyName">
        <el-input
          v-model="queryParams.strategyName"
          placeholder="请输入策略名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运营商编号" prop="operatorCode">
        <el-input
          v-model="queryParams.operatorCode"
          placeholder="请输入运营商编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="场站编码" prop="stationCode">
        <el-input
          v-model="queryParams.stationCode"
          placeholder="请输入场站编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="策略类型" prop="strategyType">
        <el-select v-model="queryParams.strategyType" placeholder="请选择策略类型" clearable>
          <el-option
            v-for="dict in dict.type.strategy_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['trendsDispatch:strategy:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['trendsDispatch:strategy:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['trendsDispatch:strategy:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="resetQuery"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="strategyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--
      <el-table-column label="策略ID" align="center" prop="id" />
-->
      <el-table-column label="策略名称" align="center" prop="strategyName" />
      <el-table-column label="运营商编号" align="center" prop="operatorCode" />
      <el-table-column label="场站编码" align="center" prop="stationCode" />
      <el-table-column label="场站名称" align="center" prop="chargeStationName" />
      <el-table-column label="变压器容量" align="center" prop="transformerCapacity" />
      <el-table-column label="阈值" align="center" prop="threshold" />
     <!-- <el-table-column label="策略类型" align="center" prop="strategyType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.strategy_type" :value="scope.row.strategyType"/>
        </template>
      </el-table-column>-->
      <el-table-column label="策略内容" align="center" prop="strategyContent" />
      <el-table-column label="描述" align="center" prop="strategyDescribe" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['trendsDispatch:strategy:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['trendsDispatch:strategy:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改场站调度策略对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="策略名称" prop="strategyName">
          <el-input v-model="form.strategyName" placeholder="请输入策略名称" />
        </el-form-item>
        <!--<el-form-item label="运营商编号" prop="operatorCode">
          <el-input v-model="form.operatorCode" placeholder="请输入运营商编号" />
        </el-form-item>
        <el-form-item label="场站编码" prop="stationCode">
          <el-input v-model="form.stationCode" placeholder="请输入场站编码" />
        </el-form-item>-->
        <el-form-item label="运营商" prop="operatorCode">
          <el-select :disabled="form.$tag === 'edit'"  v-model="form.operatorCode" filterable style="width: 340px" placeholder="请选择运营商"
                     @change="getStationAndDeviceList()">
            <el-option
              v-for="item in operatorList"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="场站" prop="stationId">
          <el-select :disabled="form.$tag === 'edit'" v-model="form.stationCode" placeholder="请选择场站" @change="getListExtend()">
            <el-option
              v-for="item in stationList"
              :key="item.chargeStationCode"
              :label="item.chargeStationName"
              :value="item.chargeStationCode">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="变压器容量" prop="transformerCapacity">
          <el-input v-model="form.transformerCapacity" placeholder="请输入变压器容量" />
        </el-form-item>
        <el-form-item label="阈值" prop="threshold">
          <el-input v-model="form.threshold" placeholder="请输入阈值" />
        </el-form-item>
        <el-form-item label="负载电表" prop="deviceCodeList">
          <el-select  v-model="form.deviceCodeList" multiple  placeholder="请选择负载电表" >
            <el-option
              v-for="item in extendList"
              :key="item.deviceId"
              :label="item.deviceName"
              :value="item.deviceId">
            </el-option>
          </el-select>
        </el-form-item>
        <!--<el-form-item label="策略类型" prop="strategyType">
          <el-select v-model="form.strategyType" placeholder="请选择策略类型">
            <el-option
              v-for="dict in dict.type.strategy_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>-->
        <el-form-item label="策略类型" prop="strategyType">
          <el-select v-model="form.strategyType" placeholder="请选择策略类型">
            <el-option
              v-for="item in ruleList"
              :key="item.ruleId"
              :label="item.ruleName"
              :value="item.ruleId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="策略内容" prop="strategyContent">
          <el-input v-model="form.strategyContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="描述" prop="strategyDescribe">
          <el-input v-model="form.strategyDescribe" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStrategy, getStrategy, delStrategy, addStrategy, updateStrategy ,getLoad } from "@/api/smart/trendsDispatch/strategy";
import { listRule } from "@/api/smart/trendsDispatch/rule";
import { listExtend } from "@/api/smart/operator/extend";
import {getOperatorList} from "@/api/smart/operator/electricityPrice";
import { listStation } from "@/api/smart/operator/station";

export default {
  name: "Strategy",
  dicts: ['strategy_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 场站调度策略表格数据
      strategyList: [],
      // 场站设备关联表格数据
      extendList: [],
      //运营商数据
      operatorList: [],
      //场站数据
      stationList: [],
      //规则数据
      ruleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        strategyName: null,
        operatorCode: null,
        stationCode: null,
        strategyType: null,
        strategyContent: null,
        strategyDescribe: null,
      },
      // 查询参数
      queryParamsExtend: {
        pageNum: 1,
        pageSize: 99,
        operatorCode: null,
        stationCode: null,
        deviceName: null,
        deviceType: 'electricityMeter',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getStation();
    this.getOperatorList();
    this.getRuleList();
  },
  methods: {
    /** 查询场站调度策略列表 */
    getList() {
      this.loading = true;
      listStrategy(this.queryParams).then(response => {
        this.strategyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getRuleList(){
      listRule(this.queryParams).then(response => {
        this.ruleList = response.rows;
      });
    },
    getListExtend(){
      this.queryParamsExtend.stationCode= this.form.stationCode
      this.getLoadElectricityMeter();
      listExtend(this.queryParamsExtend).then(response => {
        this.extendList = response.rows;
      });
    },
    getLoadElectricityMeter(){
      getLoad(this.queryParamsExtend).then(response => {
        this.form.deviceCodeList=response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        strategyName: null,
        operatorCode: null,
        stationCode: null,
        strategyType: null,
        strategyContent: null,
        strategyDescribe: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deviceCodeList:null,
        transformerCapacity:null,
        threshold:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.strategyName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.$tag = 'add';
      this.getRuleList();
      this.title = "添加场站调度策略";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStrategy(id).then(response => {
        this.form = response.data;
        this.form.$tag = 'edit';
        this.getListExtend();
        this.open = true;
        this.getRuleList();
        this.title = "修改场站调度策略";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStrategy(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStrategy(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const names = row.strategyName || this.names;
      this.$modal.confirm('是否确认删除策略名称为"' + names + '"的数据项？').then(function() {
        return delStrategy(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('trendsDispatch/strategy/export', {
        ...this.queryParams
      }, `strategy_${new Date().getTime()}.xlsx`)
    },
    /** 查询场站和设备列表 */
    async getStation() {
      this.queryParams.operatorCode=this.form.operatorCode;
      this.loading = true;
      await listStation(this.queryParams).then(response => {
        this.stationList = response.rows;
      });
      this.loading = false;
    },
    getStationAndDeviceList(){
      this.queryParams.operatorCode=this.form.operatorCode;
      listStation(this.queryParams).then(response => {
        this.stationList = response.rows;
      });
    },
    /*查询运营商列表*/
    getOperatorList() {
      getOperatorList().then(response => {
        this.operatorList = response.data;
      })
    },
  }
};
</script>
